<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
            <el-form-item label="表名:" prop="tableName"
                :rules="[
                  {required: true, message:'表名不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.tableName" placeholder="输入表名"     ></el-input>
           </el-form-item>
            <el-form-item label="表注释:" prop="tableDescribe"
                :rules="[
                  {required: true, message:'表注释不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.tableDescribe" placeholder="输入表说明"     ></el-input>
           </el-form-item>
            <el-form-item label="表类型:" prop="tableType"
                :rules="[
                  {required: true, message:'表类型不能为空', trigger:'blur'}
                 ]">
                <el-select clearable v-model="inputForm.tableType" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('tableType')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
          <el-form-item label="执行视图/SQL语句:" prop="sqlTest">
            <el-input v-model="inputForm.sqlText"  type="textarea" row="4" placeholder="填写执行视图/SQL语句"     ></el-input>
          </el-form-item>
          <el-form-item label="单数据包大小:" prop="dataSize"
                        :rules="[
                          {required: true, message:'单数据包大小不能为空', trigger:'blur'},
                          {
                            type: 'number',
                            message: '请输入正整数',
                            trigger: 'blur',
                            transform(value) {
                              if(value !== null && value !== '') {
                                if (String(value).trim() === '' || Number(value) <= 0) {
                                  return false
                                }else if (String(value).indexOf('.') !== -1 || String(value).indexOf('-') !== -1) {
                                  return false
                                }else{
                                  return Number(value)
                                }
                              }else {
                                return null
                              }
                            }
                           }
                         ]">
            <el-input v-model="inputForm.dataSize" placeholder="请输入正整数"     ></el-input>
          </el-form-item>
          <el-form-item  prop="isPriority">
            <el-checkbox v-model="checked">是否优先</el-checkbox>
          </el-form-item>
<!--        <el-col :span="12">-->
<!--            <el-form-item label="表结构时间戳" prop="tableTimeStamp"-->
<!--                :rules="[-->
<!--                  {required: true, message:'表结构时间戳不能为空', trigger:'blur'}-->
<!--                 ]">-->
<!--              <el-input v-model="inputForm.tableTimeStamp" placeholder="请填写表结构时间戳"     ></el-input>-->
<!--           </el-form-item>-->
<!--        </el-col>-->
<!--        <el-col :span="12">-->
<!--            <el-form-item label="表内容时间戳" prop="itemTimeStamp"-->
<!--                :rules="[-->
<!--                  {required: true, message:'表内容时间戳不能为空', trigger:'blur'}-->
<!--                 ]">-->
<!--              <el-input v-model="inputForm.itemTimeStamp" placeholder="请填写表内容时间戳"     ></el-input>-->
<!--           </el-form-item>-->
<!--        </el-col>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        checked: true,
        inputForm: {
          id: '',
          tableName: '',
          tableDescribe: '',
          tableType: '',
          sqlText: '',
          dataSize: '',
          isPriority: 0
        }
      }
    },
    components: {
    },
    methods: {
    	handleClose(done) {
			this.$Msg.confirm({
				title:'您确定要关闭吗？',
				content:this.GLOBAL.FORM_CLOSE_TIPS,
				confirmCall:()=>{
					done();
				}
			})
		},
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建下载表管理`
        } else if (method === 'edit') {
          this.title = '修改下载表管理'
        } else if (method === 'view') {
          this.title = '查看下载表管理'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/api/mqttTable/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.mqttTable)
              if (data.mqttTable.isPriority === 0) {
                this.checked = false
              } else if (data.mqttTable.isPriority === 1) {
                this.checked = true
              }
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        console.log(this.checked)
        if (this.checked === true) {
          this.inputForm.isPriority = 1
        } else if (this.checked === false) {
          this.inputForm.isPriority = 0
        }
        console.log(this.inputForm.isPriority)
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/api/mqttTable/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
